package com.zhcd.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhcd.sys.mapper.DoctorMapper;
import com.zhcd.sys.pojo.Detail;
import com.zhcd.sys.mapper.DetailMapper;
import com.zhcd.sys.pojo.Doctor;
import com.zhcd.sys.pojo.DoctorDetail;
import com.zhcd.sys.pojo.Mattess;
import com.zhcd.sys.service.DetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author feifei
 * @since 2021-11-29
 */
@Service
public class DetailServiceImpl extends ServiceImpl<DetailMapper, Detail> implements DetailService {
    @Autowired
    private DetailMapper detailMapper;
    @Autowired
    private DoctorMapper doctorMapper;

    //查询全部收费明细
    @Override
    public List<DoctorDetail> getAll_DoctorDetail() {
        List<DoctorDetail> doctorDetails =new ArrayList<>();
        List<Detail> details = detailMapper.selectList(null);
        for (Detail detail:details){
            DoctorDetail doctorDetail = new DoctorDetail();
            doctorDetail.setDetailid(detail.getDetailid());
            doctorDetail.setMoney(detail.getMoney());
            doctorDetail.setPeopleid(detail.getPeopleid());
            doctorDetail.setName(detail.getName());
            doctorDetail.setDepartmentid(detail.getDepartmentid());
            doctorDetail.setDepartmentname(detail.getDepartmentname());
            doctorDetail.setDoctorid(detail.getDoctorid());
            doctorDetail.setDoctorname(detail.getDoctorname());
            doctorDetail.setCreatetime(detail.getCreatetime());
            doctorDetail.setPayway(detail.getPayway());
            doctorDetail.setPaystatus(detail.getPaystatus());
            doctorDetail.setDoctorphone((doctorMapper.selectById(detail.getDoctorid())).getDoctorphone());
            doctorDetails.add(doctorDetail);
        }
        return doctorDetails;
    }

    //按照条(医生、时间段)件查询收费明细
    @Override
    public List<DoctorDetail> getList_DoctorDetail(String doctorName, String startTime, String endTime) {
        List<DoctorDetail> doctorDetails = new ArrayList<>();
        QueryWrapper<Detail> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("DoctorName",doctorName).or().between("CreateTime",startTime,endTime);
        List<Detail> details1 = detailMapper.selectList(objectQueryWrapper);
        for (Detail detail:details1){
            DoctorDetail doctorDetail = new DoctorDetail();
            doctorDetail.setDetailid(detail.getDetailid());
            doctorDetail.setMoney(detail.getMoney());
            doctorDetail.setPeopleid(detail.getPeopleid());
            doctorDetail.setName(detail.getName());
            doctorDetail.setDepartmentid(detail.getDepartmentid());
            doctorDetail.setDepartmentname(detail.getDepartmentname());
            doctorDetail.setDoctorid(detail.getDoctorid());
            doctorDetail.setDoctorname(detail.getDoctorname());
            doctorDetail.setCreatetime(detail.getCreatetime());
            doctorDetail.setPayway(detail.getPayway());
            doctorDetail.setPaystatus(detail.getPaystatus());
            doctorDetail.setDoctorphone((doctorMapper.selectById(detail.getDoctorid())).getDoctorphone());
            doctorDetails.add(doctorDetail);
        }
        return doctorDetails;
    }

    //按照条件(医生电话)查找收费明细
    @Override
    public List<DoctorDetail> getPhone_DoctorDetail(String doctorPhone) {
        List<DoctorDetail> doctorDetails = new ArrayList<>();
        QueryWrapper<Doctor> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("DoctorPhone",doctorPhone);
        Doctor doctor = doctorMapper.selectOne(objectQueryWrapper);
        if(doctor!=null){
            QueryWrapper<Detail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("DoctorId",doctor.getDoctorid());
            List<Detail> details = detailMapper.selectList(queryWrapper);
            for (Detail detail:details){
                DoctorDetail doctorDetail = new DoctorDetail();
                doctorDetail.setDetailid(detail.getDetailid());
                doctorDetail.setMoney(detail.getMoney());
                doctorDetail.setPeopleid(detail.getPeopleid());
                doctorDetail.setName(detail.getName());
                doctorDetail.setDepartmentid(detail.getDepartmentid());
                doctorDetail.setDepartmentname(detail.getDepartmentname());
                doctorDetail.setDoctorid(detail.getDoctorid());
                doctorDetail.setDoctorname(detail.getDoctorname());
                doctorDetail.setCreatetime(detail.getCreatetime());
                doctorDetail.setPayway(detail.getPayway());
                doctorDetail.setPaystatus(detail.getPaystatus());
                doctorDetail.setDoctorphone(doctor.getDoctorphone());
                doctorDetails.add(doctorDetail);
            }

        }
        return doctorDetails;
    }

    //按条件查询交费明细
    @Override
    public List<Detail> getAll_Detail(String name) {
        QueryWrapper<Detail> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("Name",name);
        return detailMapper.selectList(objectQueryWrapper);
    }
}
